<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>

<% String fileNum = request.getParameter("fileNum");
    String filePath = request.getParameter("filePath");
    if (fileNum == null) {
        fileNum = "1";
    }
    if (filePath == null) {
        filePath = "";
    }
    String upLoadCode = request.getParameter("upLoadCode");
    if (upLoadCode == null) {
        upLoadCode = "";
    }
    String uploadSysCd = request.getParameter("uploadSysCd");
    if (uploadSysCd == null) {
        uploadSysCd = "";
    }
    String costomerPath = request.getParameter("costomerPath");
    if (costomerPath == null) {
        costomerPath = "";
    }
    String rootPath = request.getParameter("rootPath");
    if (rootPath == null) {
        rootPath = "";
    }
%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>

<div id="fileFields" class="upfileBoxs">
    <span class="sq-button sq-button-hasicon" id="btnUpload"><i class="sqBtnIco_9"></i>上传</span>
</div>
<input type="hidden" id="fileNum" value="<%=fileNum %>"/>
<input type="hidden" id="filePath" value="<%=filePath %>"/>
<input type="hidden" id="upLoadCode" value="<%=upLoadCode %>"/>
<input type="hidden" id="uploadSysCd" value="<%=uploadSysCd %>"/>
<input type="hidden" id="costomerPath" value="<%=costomerPath %>"/>
<input type="hidden" id="rootPath" value="<%=rootPath %>"/>


<script type="text/javascript">
    var upload_result_params,
        counts = $("#fileNum").val(),
        upLoadCode = $("#upLoadCode").val(),
        uploadSysCd = $("#uploadSysCd").val(),
        costomerPath = $("#costomerPath").val(),
        rootPath = $("#rootPath").val()
    ;

    $(function () {
        console.log("upLoadCode:"+upLoadCode);
        console.log("uploadSysCd:"+uploadSysCd);
        console.log("costomerPath:"+costomerPath);
        console.log("rootPath:"+rootPath);
        var fileItems = [];
        for (var i = 1; i <= counts; i++) {
            $Html = CreatUploadInputBox({
                id: "file" + i,  //id不跟着序号走！
                //clearLine: true,
                deletable: false
            });
            $("#fileFields").prepend($Html);
        }

        //上传
        $("#btnUpload").click(function () {
            uploadFile();
        });
        CreatUploadInputBox({
            target: ".fileup",
        });
    });

    function setUpfileCount() { //设置序号
        $(".UpfileCount").each(function (index, el) {
            $(this).html(index + 1);
        });
    }

    function uploadFile(id) {
        //获取file的全部id  
        var uplist = $("input[id^=file]");
        //alert(JSON.stringify(uplist));
        if (uplist.length == 0) {
            $.ligerDialog.SQalert({content: "没有要上传的内容！", SQ_type: "warning"});
            return;
        }
        var arrId = [];
        var fileNames = [];//用于判断是否有重名文件
        for (var i = 0; i < uplist.length; i++) {
            if (uplist[i].value) {
                arrId[i] = uplist[i].id;
                if ($.inArray(uplist[i].value, fileNames) >= 0) {
                    $.ligerDialog.SQalert({content: uplist[i].value + "文件重名！", SQ_type: "warning"});
                    return;
                }
                fileNames[i] = uplist[i].value;
            }
        }
        var _filePath = $("#filePath").val();
        var _sysCd = $("#sysCd").val();
        var _opTypCd = $("#opTypCd").val();
        var _verNo = $("#verNo").val();
        var _upLoadCode = $("#upLoadCode").val();
        var _uploadSysCd = $("#uploadSysCd").val();
        var _costomerPath = $("#costomerPath").val();
        var _rootPath = $("#rootPath").val();
        console.log("_upLoadCode:"+_upLoadCode);
        console.log("_uploadSysCd:"+_uploadSysCd);
        console.log("_costomerPath:"+_costomerPath);
        console.log("_rootPath:"+_rootPath);
        var wait_dialog = $.ligerDialog.SQalert({content: '上传中,请稍后...', SQ_type: "waitting"});
        $.ajaxFileUpload({
            url: '<%=path%>/system/sysulfilesrec/upload.do',
            secureuri: false,
            fileElementId: arrId,  //这里不在是以前的id了，要写成数组的形式！  
            dataType: 'json',
            data: {filePath: _filePath, sysCd: _sysCd, opTypCd: _opTypCd, verNo: _verNo,upLoadCode:_upLoadCode,uploadSysCd:_uploadSysCd,costomerPath:_costomerPath,rootPath:_rootPath},
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                    uploadCallback(msg.data);
                    uploadCallback_2(msg.data);
                    upload_result_params = msg.data;
                    $('.upfileBoxs [type=file]').each(function (index, el) {
                        $(el).afterUploadFile({
                            filenameField: 'fileNm', //默认值就是fileNm
                            filePathField: 'filePath', //默认值就是filePath
                            files_DATA: msg.data
                        });
                    });
                } else {
                    top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            },
            complete: function () {
                //重新初始化上传标签
                CreatUploadInputBox({
                    target: "input[id^=file]",
                    placeholder: null//不会清除文件,没有这句则会清除文件
                });
            }
        });
    }

    //获取上传结果参数
    function getUploadResultParams() {
        return upload_result_params;
    }

    //上传回调方法
    function uploadCallback(_data) {

    }

    function uploadCallback_2(_data) {

    }

    //清除上传结果参数
    function clearUploadResultParams() {
        //清除文件
        CreatUploadInputBox({
            target: "input[id^=file]"
        });
        upload_result_params = [];//设置参数为空
    }
</script>
